package com.twoGroup.boot.service.imp;


import com.twoGroup.boot.mapper.CommentMapper;
import com.twoGroup.boot.pojo.Comment;
import com.twoGroup.boot.pojo.Page;
import com.twoGroup.boot.pojo.ResultDate;
import com.twoGroup.boot.service.CommentService;
import com.twoGroup.boot.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName
 * @Description TODO
 * @Author Xuyujie
 * @Date 2021/6/15 21:47
 * @Version 1.0
 **/
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public ResultDate doComment(String commentContent, String userId, String commentLevel, String goodsId) {
        ResultDate resultDate = null;
        if (!(CommonUtils.isNotnull(userId) && CommonUtils.isNumeric(userId) && CommonUtils.ParamIsInRange(userId))) {
            return resultDate = ResultDate.fail("参数错误");
        }
        if (!(CommonUtils.isNotnull(commentLevel) && CommonUtils.isNumeric(commentLevel) && CommonUtils.ParamIsInRange(commentLevel))) {
            return resultDate = ResultDate.fail("参数信息错误");
        }
        if (!(CommonUtils.isNotnull(goodsId) && CommonUtils.isNumeric(goodsId) && CommonUtils.ParamIsInRange(goodsId))) {
            return resultDate = ResultDate.fail("参数错误");
        }
        if (!CommonUtils.isNotnull(commentContent)) {
            return resultDate = ResultDate.fail("评论内容为空！");
        }
        if (commentContent.length() > 200) {
            return resultDate = ResultDate.fail("评论内容过长！");
        }
        Comment comment = new Comment();
        Date date = new Date(System.currentTimeMillis());
        comment.setCommentContent(commentContent);
        comment.setCommentDate(date);
        comment.setUserId(Integer.parseInt(userId));
        comment.setCommentLevel(Integer.parseInt(commentLevel));
        comment.setGoodsId(Integer.parseInt(goodsId));
        int result = commentMapper.doComment(comment);
        if (result == 1) {
            return resultDate = ResultDate.success("评论成功！");
        } else {
            return resultDate = ResultDate.fail("评论失败！");
        }
    }


    @Override
    public ResultDate queryForAllComments(Integer goodsId, Integer pageNow, Integer pageSize) {
        ResultDate resultDate = null;
        if (goodsId == null || goodsId < 0 || goodsId > Integer.MAX_VALUE) {
            return resultDate = ResultDate.fail("参数信息错误！");
        }
        if (pageNow == null || pageNow < 0 || pageNow > Integer.MAX_VALUE) {
            return resultDate = ResultDate.fail("参数信息错误！");
        }
        if (pageSize == null || pageSize < 0 || pageSize > Integer.MAX_VALUE) {
            return resultDate = ResultDate.fail("参数信息错误！");
        }
        Page<Map<String, Object>> page = new Page<>();
        page.setPageNow(pageNow);
        page.setPageSize(pageSize);
        page.setStart((pageNow - 1) * pageSize);
        page.setPageTotalCount(commentMapper.queryForAllCount(goodsId));
        page.setPageTotal(page.getPageTotal());
        List<Map<String, Object>> list = commentMapper.queryForComment(goodsId, pageNow, pageSize);
        page.setList((ArrayList<Map<String, Object>>) list);
        return resultDate = ResultDate.success("查询成功！", page);
    }

    @Override
    public ResultDate queryForUserComment(Integer userId, Integer pageNow, Integer pageSize) {
        ResultDate resultDate = null;
        if (userId == null || userId < 0 || userId > Integer.MAX_VALUE) {
            return resultDate = ResultDate.fail("参数信息错误！");
        }
        if (pageNow == null || pageNow < 0 || pageNow > Integer.MAX_VALUE) {
            return resultDate = ResultDate.fail("参数信息错误！");
        }
        if (pageSize == null || pageSize < 0 || pageSize > Integer.MAX_VALUE) {
            return resultDate = ResultDate.fail("参数信息错误！");
        }
        Page<Map<String, Object>> page = new Page<>();
        page.setPageNow(pageNow);
        page.setPageSize(pageSize);
        page.setStart((pageNow - 1) * pageSize);
        page.setPageTotalCount(commentMapper.queryForAllUserCount(userId));
        page.setPageTotal(page.getPageTotal());
        List<Map<String, Object>> list = commentMapper.queryForUserComment(userId, pageNow, pageSize);
        page.setList((ArrayList<Map<String, Object>>) list);
        return ResultDate.success("查询成功！", page);
    }

    @Override
    public ResultDate queryForNum(Integer userId) {
        ResultDate resultDate;
        if (userId == null || userId < 0 || userId > Integer.MAX_VALUE) {
            return resultDate = ResultDate.fail("参数信息错误！");
        }
        int result = commentMapper.queryForNotComment(userId);
        resultDate = ResultDate.success("查询成功!", result);
        return resultDate;
    }

    @Override
    public ResultDate queryForNotComment(Integer userId, Integer pageNow, Integer pageSize) {
        ResultDate resultDate = null;
        if (userId == null || userId < 0 || userId > Integer.MAX_VALUE) {
            return resultDate = ResultDate.fail("参数信息错误！");
        }
        if (pageNow == null || pageNow < 0 || pageNow > Integer.MAX_VALUE) {
            return resultDate = ResultDate.fail("参数信息错误！");
        }
        if (pageSize == null || pageSize < 0 || pageSize > Integer.MAX_VALUE) {
            return resultDate = ResultDate.fail("参数信息错误！");
        }
        Page<Map<String, Object>> page = new Page<>();
        page.setPageNow(pageNow);
        page.setPageSize(pageSize);
        page.setStart((pageNow - 1) * pageSize);
        page.setPageTotalCount(commentMapper.queryForNotComment(userId));
        page.setPageTotal(page.getPageTotal());
        List<Map<String, Object>> list = commentMapper.queryForUserNotComment(userId, pageNow, pageSize);
        page.setList((ArrayList<Map<String, Object>>) list);
        return ResultDate.success("查询成功！", page);
    }
}
